/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Unless required by applicable law or
 * agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package org.rhok.pdx.ncn.background;

import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.rhok.pdx.ncn.R;
import org.rhok.pdx.ncn.datatypes.LocationData;
import org.rhok.pdx.ncn.datatypes.MapOverlay;
import org.rhok.pdx.ncn.storage.NCNDatabaseManager;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * Utility to drop dots on the map relating to signal intensity.
 * 
 * @author pfritzsche
 */
public class MapPlotterTask extends AsyncTask<Void, Void, Void> {
	/**
	 * The Interface MapPlotterContext.
	 */
	public interface MapPlotterContext {
		public ConcurrentLinkedQueue<LocationData> locs();
		public Activity app();
		public MapView mapView();
		public NCNDatabaseManager db();
	}

	private Drawable greenDrawable;
	private Drawable yellowDrawable;
	private Drawable redDrawable;
	private MapPlotterContext context;

	/**
	 * Instantiates a new map plotter.
	 *
	 * @param context the context
	 */
	public MapPlotterTask(MapPlotterContext context) {
		this.context = context;

		greenDrawable = context.app().getResources().getDrawable(R.drawable.greendot);
		yellowDrawable = context.app().getResources().getDrawable(R.drawable.yellowdot);
		redDrawable = context.app().getResources().getDrawable(R.drawable.reddot);
	}
	
	@Override
	protected void onPreExecute() {
		context.db().open();
    }

	@Override
	protected Void doInBackground(Void... params) {
		while (!isCancelled()) {
			if (context.locs().size() != 0) {
				publishProgress();
			}
		}
		return null;
	}

	@Override
	protected void onProgressUpdate(Void... params) {
		List<Overlay> overlays = context.mapView().getOverlays();
		MapOverlay overlay = new MapOverlay(greenDrawable);
		while (!context.locs().isEmpty()) {
			LocationData loc = context.locs().poll();
			GeoPoint point = new GeoPoint((int) (loc.lat() * 1E6), (int) (loc.lng() * 1E6));
			OverlayItem item = new OverlayItem(point, "", "");

			if (loc.signal() < 10) {
				item.setMarker(redDrawable);
			} else if (loc.signal() < 20) {
				item.setMarker(yellowDrawable);
			} else {
				item.setMarker(greenDrawable);
			}
			overlay.addOverlay(item);
		}
		overlays.add(overlay);
//		context.mapView().invalidate();
		context.mapView().performClick();
	}
	
	@Override
	protected void onPostExecute(Void result) {
		context.db().close();
    }
}
